{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "681dd904",
   "metadata": {},
   "source": [
    "## 类：面向对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "e3bbc20d",
   "metadata": {},
   "outputs": [],
   "source": [
    "class people:\n",
    "    '帮助信息：XXXXX'\n",
    "    # 所有的示例都会共享\n",
    "    number = 100\n",
    "    # 构造函数，初始化方法，当创建一个类的时候，首先会调用它\n",
    "    def __init__(self,name,age):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "    def dispay(self):\n",
    "        print('number =:',people.number)\n",
    "        \n",
    "    def dispay_name(self):\n",
    "        print(self.name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "52aacc13",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'帮助信息：XXXXX'"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "people.__doc__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "1774fc44",
   "metadata": {},
   "outputs": [],
   "source": [
    "p1 = people('tangyudi',30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "04b8ff11",
   "metadata": {},
   "outputs": [],
   "source": [
    "p2 = people(\"python\",'40')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "56d07245",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'tangyudi'"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p1.name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "bda2ee84",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'python'"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p2.name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "2bbc11c4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "number =: 100\n"
     ]
    }
   ],
   "source": [
    "p1.dispay()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "45ac43ff",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "number =: 100\n"
     ]
    }
   ],
   "source": [
    "p2.dispay()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "9ab0cb9d",
   "metadata": {},
   "outputs": [],
   "source": [
    "p2.name ='hello'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "0561b43a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hello'"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p2.name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "709da586",
   "metadata": {},
   "outputs": [],
   "source": [
    "del p2.name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "93231c22",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'people' object has no attribute 'name'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[40], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m p2\u001b[38;5;241m.\u001b[39mname\n",
      "\u001b[1;31mAttributeError\u001b[0m: 'people' object has no attribute 'name'"
     ]
    }
   ],
   "source": [
    "p2.name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "510ddac7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "hasattr(p1,'name')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "9658abb5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "hasattr(p1,'sex')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "797857c4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'tangyudi'"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "getattr(p1,'name')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "5b3fa501",
   "metadata": {},
   "outputs": [],
   "source": [
    "setattr(p1,'name','yuding')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "3adf55ce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'yuding'"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "getattr(p1,'name')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "3c6920f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "delattr(p1,'name')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "c6105a28",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'people' object has no attribute 'name'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[47], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28mgetattr\u001b[39m(p1,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mname\u001b[39m\u001b[38;5;124m'\u001b[39m)\n",
      "\u001b[1;31mAttributeError\u001b[0m: 'people' object has no attribute 'name'"
     ]
    }
   ],
   "source": [
    "getattr(p1,'name')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "d06e55e1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "帮助信息：XXXXX\n",
      "people\n",
      "__main__\n",
      "(<class 'object'>,)\n",
      "{'__module__': '__main__', '__doc__': '帮助信息：XXXXX', 'number': 100, '__init__': <function people.__init__ at 0x000001FBA045B880>, 'dispay': <function people.dispay at 0x000001FBA04587C0>, 'dispay_name': <function people.dispay_name at 0x000001FBA0518EA0>, '__dict__': <attribute '__dict__' of 'people' objects>, '__weakref__': <attribute '__weakref__' of 'people' objects>}\n"
     ]
    }
   ],
   "source": [
    "print(people.__doc__)\n",
    "print(people.__name__)\n",
    "print(people.__module__)\n",
    "print(people.__bases__)\n",
    "print(people.__dict__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "2f382d6a",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Parent:\n",
    "    number = 100\n",
    "    def __init__(self):\n",
    "        print('调用父类构造函数')\n",
    "    def parentM(self):\n",
    "        print('调用父类方法')\n",
    "    def setAttr(self,attr):\n",
    "        Parent.parentAttr = attr\n",
    "    def getAttr(self):\n",
    "        print('父类属性：',Parent.parentAttr)\n",
    "    def newM(self):\n",
    "        print('父类要被重写的方法')\n",
    "class child(Parent): # 定义子类\n",
    "    def __init__(self):\n",
    "        print('调用子类构造函数')\n",
    "    def childM(self):\n",
    "        print('调用子类方法')\n",
    "    def newM(self):\n",
    "        print('子类给它改掉了')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "5306de1a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "调用子类构造函数\n",
      "调用子类方法\n",
      "调用父类方法\n",
      "父类属性： 100\n",
      "子类给它改掉了\n"
     ]
    }
   ],
   "source": [
    "c = child()\n",
    "c.childM()\n",
    "c.parentM()\n",
    "c.setAttr(100)\n",
    "c.getAttr()\n",
    "c.newM()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3741604b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
